﻿using LD.Host;
using LD.Model;
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;

namespace LD
{
    public class WipHelper 
    {
        private static WipHelper instance;
        private static object locker = new object();
        private string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config\\wip.config");
        public static WipHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                            instance = new WipHelper();
                    }
                }
                return instance;
            }
        }
        private TB_WIP _wipData = null;
        public TB_WIP WipData
        {
            get { return _wipData; }
            set { _wipData = value; OnPropertyChanged(); }
        }
        public void Load()
        {
            if (File.Exists(_filePath))
            {
                _wipData = JsonSerializeHelper.Load<TB_WIP>(_filePath);
            }

            if (_wipData == null)
                _wipData = new TB_WIP();
        }
        public void Save()
        {
            JsonSerializeHelper.Save(WipData, _filePath);
        }
        public event PropertyChangedEventHandler PropertyChanged;

        //通知所有的观察者去更新页面
        //CallerMemberName : 外部调用OnPropertyChanged方法时，不用传参数，自动获取调用者的方法名
        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
